<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="内容名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入内容名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="内容类型" prop="courseItemType">
        <el-select
          v-model="queryParams.courseItemType"
          placeholder="请选择课程类型"
          clearable
          class="!w-240px"
        >
          <el-option label="视频内容" :value="1" />
          <el-option label="图文内容" :value="2" />
          <el-option label="题目内容" :value="3" />
          <el-option label="直播内容" :value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否免费" prop="isFree">
        <el-select v-model="queryParams.isFree" placeholder="请选择状态" clearable class="!w-240px">
          <el-option label="免费" value="true" />
          <el-option label="付费" value="false" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否启用" prop="itemStatus">
        <el-select v-model="queryParams.isFree" placeholder="请选择状态" clearable class="!w-240px">
          <el-option label="启用" :value="1" />
          <el-option label="禁用" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="addCourseItem"
          v-hasPermi="['course:course-item:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['course:course-item:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="内容名称" align="center" prop="itemName" />
      <el-table-column
        label="内容类型"
        align="center"
        prop="courseItemType"
        :formatter="typeFormatter"
      />
      <el-table-column label="内容描述" align="center" prop="itemDesc" />
      <el-table-column label="是否免费" align="center" prop="isFree" :formatter="freeFormatter" />
      <el-table-column label="是否启用" align="center" prop="itemStatus">
        <template #default="scope">
          <el-tag
            class="ml-2"
            type="success"
            :color="statusFormatter(scope.row.itemStatus).color"
            >{{ statusFormatter(scope.row.itemStatus).str }}</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['course:course-item:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['course:course-item:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：修改 -->
  <CourseItemForm ref="formRef" @success="getList" />
  <!-- 表单弹窗：添加 -->
  <AddCourseItemForm ref="addFormRef" @success="getList" />
</template>

<script setup lang="ts" name="Service">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import * as CourseOpenApi from '@/mock/course/course-item'
import CourseItemForm from './CourseItemForm.vue'
import AddCourseItemForm from './AddCourseItemForm.vue'
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  id: null,
  itemName: null,
  itemDesc: null,
  courseItemType: null,
  courseItemTypeId: null,
  isFree: null,
  itemStatus: null,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await CourseOpenApi.getCourseItemPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 添加操作 */
const addFormRef = ref()
const addCourseItem = () => {
  addFormRef.value.open()
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await CourseOpenApi.deleteCourse(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  // try {
  //   // 导出的二次确认
  //   await message.exportConfirm()
  //   // 发起导出
  //   exportLoading.value = true
  //   const data = await CourseOpenApi.exportService(queryParams)
  //   download.excel(data, '服务信息.xls')
  // } catch {
  // } finally {
  //   exportLoading.value = false
  // }
}

/** 表格项状态格式化  1 启用 2 禁用*/
const statusFormatter = (val) => {
  let str = ''
  let color = ''
  switch (val) {
    case 1:
      str = '启用'
      color = '#ecf5ff'
      break
    case 2:
      str = '禁用'
      color = '#f0f9eb'
      break
  }
  return {
    str,
    color
  }
}

const freeFormatter = (row, column, cellValue, index) => {
  return cellValue ? '免费' : '付费'
}

const typeFormatter = (row, column, cellValue, index) => {
  let type = {
    1: '视频',
    2: '图文',
    3: '题目',
    4: '直播'
  }
  return type[cellValue]
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
